package ru.ssau;

import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import lab.domain.*;

/**
 *
 * @author Smike
 */
public class CollectionDAOBean implements SessionBean {

    private SessionContext context;
    private Connection connection;
    private DataSource datasource;
    
    public void setSessionContext(SessionContext ctx) throws EJBException, RemoteException {
        context = ctx;
        try {
            datasource = (DataSource) new InitialContext().lookup("oracleDS");
        } catch (NamingException ex) {
            throw new EJBException(ex);
        }
    }

    public void ejbRemove() throws EJBException, RemoteException {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException ex) {
                Logger.getLogger(CollectionDAOBean.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public void ejbActivate() throws EJBException, RemoteException {
    }

    public void ejbPassivate() throws EJBException, RemoteException {
    }
    
    public String getDemo() {
        return "Hello World!";
    }
    
    public Collection<ID3v1Tag> findTags() {
        try {
            return DatabaseHelper.findTags(getConnection());
        } catch (SQLException ex) {
            throw new RuntimeException(ex);
        }
    }
       
    public Collection<Artist> findArtists() {
        try {
            return DatabaseHelper.findArtists(getConnection());
        } catch (SQLException ex) {
            throw new RuntimeException(ex);
        }
    }
    
    public Artist findArtist(int id) {
        try {
            return DatabaseHelper.findArtist(getConnection(), id);
        } catch (SQLException ex) {
            throw new RuntimeException(ex);
        }
    }
    
    public Collection<Genre> findGenres() {
        try {
            return DatabaseHelper.findGenres(getConnection());
        } catch (SQLException ex) {
            throw new RuntimeException(ex);
        }
    }
    
    public Genre findGenre(int id) {
        try {
            return DatabaseHelper.findGenre(getConnection(), id);
        } catch (SQLException ex) {
            throw new RuntimeException(ex);
        }
    }
    
    public Collection<SoundRecordingStudio> findStudios() {
        try {
            return DatabaseHelper.findStudios(getConnection());
        } catch (SQLException ex) {
            throw new RuntimeException(ex);
        }
    }
    
    public SoundRecordingStudio findStudio(int id) {
        try {
            return DatabaseHelper.findStudio(getConnection(), id);
        } catch (SQLException ex) {
            throw new RuntimeException(ex);
        }
    }
    
    public Collection<Track> findTracks() {
        try {
            return DatabaseHelper.findTracks(getConnection());
        } catch (SQLException ex) {
            throw new RuntimeException(ex);
        }
    }
    
    public Track findTrack(int id) {
        try {
            return DatabaseHelper.findTrack(getConnection(), id);
        } catch (SQLException ex) {
            throw new RuntimeException(ex);
        }
    }
    
    public void updateStudio(SoundRecordingStudio studio) {
        try {
            DatabaseHelper.updateStudio(getConnection(), studio);
        } catch (SQLException ex) {
            throw new RuntimeException(ex);
        }
    }
    
    public void updateTrack(Track track) {
        try {
            DatabaseHelper.updateTrack(getConnection(), track);
        } catch (SQLException ex) {
            throw new RuntimeException(ex);
        }
    }
    
    public void updateGenre(Genre genre) {
        try {
            DatabaseHelper.updateGenre(getConnection(), genre);
        } catch (SQLException ex) {
            throw new RuntimeException(ex);
        }
    }
    
    public void updateArtist(Artist artist) {
        try {
            DatabaseHelper.updateArtist(getConnection(), artist);
        } catch (SQLException ex) {
            throw new RuntimeException(ex);
        }
    }
    
    public void insertStudio(SoundRecordingStudio studio) {
        try {
            DatabaseHelper.insertStudio(getConnection(), studio);
        } catch (SQLException ex) {
            throw new RuntimeException(ex);
        }
    }
    
    public void insertTrack(Track track) {
        try {
            DatabaseHelper.insertTrack(getConnection(), track);
        } catch (SQLException ex) {
            throw new RuntimeException(ex);
        }
    }
    
    public void insertGenre(Genre genre) {
        try {
            DatabaseHelper.insertGenre(getConnection(), genre);
        } catch (SQLException ex) {
            throw new RuntimeException(ex);
        }
    }
    
    public void insertArtist(Artist artist) {
        try {
            DatabaseHelper.insertArtist(getConnection(), artist);
        } catch (SQLException ex) {
            throw new RuntimeException(ex);
        }
    }
    
    public void deleteStudio(SoundRecordingStudio studio) {
        try {
            DatabaseHelper.deleteStudio(getConnection(), studio);
        } catch (SQLException ex) {
            throw new RuntimeException(ex);
        }
    }
    
    public void deleteTrack(Track track) {
        try {
            DatabaseHelper.deleteTrack(getConnection(), track);
        } catch (SQLException ex) {
            throw new RuntimeException(ex);
        }
    }
    
    public void deleteGenre(Genre genre) {
        try {
            DatabaseHelper.deleteGenre(getConnection(), genre);
        } catch (SQLException ex) {
            throw new RuntimeException(ex);
        }
    }
    
    public void deleteArtist(Artist artist) {
        try {
            DatabaseHelper.deleteArtist(getConnection(), artist);
        } catch (SQLException ex) {
            throw new RuntimeException(ex);
        }
    }
    

    private Connection getConnection() {
        try {
            connection = datasource.getConnection();
            return connection;
        } catch (SQLException ex) {
            throw new RuntimeException(ex);
        }
    }
    
}
